package com.itboye.mybatis.helper;

/**
 * SecurityCodeHelper.java
 * Created on  2016/7/29 11:03
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/7/29      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
public class BySecurityCodeHelper {
    /**
     *
     * @param length 生成的字符串的位数
     * @return
     */
    public  static  String generate(int length){

//        String[] numbers = {"0","1","2","3","4","5","6","7","8","9"};
//        String random = "";
//
//        for (int i=0;i<length;i++){
//            int randIndex = (int) (Math.random() * numbers.length);
//            random += numbers[randIndex];
//        }
//
//        return random;

//2. 这个方法 更快些
        int pow = (int) Math.pow(10,length);

        double rand = Math.random() * pow;

        Integer intRand = Integer.parseInt("" + (int)(rand));

        String strRand = intRand.toString();
        if(strRand.length() < length){
            for (int i=0;i<length - strRand.length();i++){
                strRand = strRand.concat("0");
            }
        }

        return strRand;
    }
}
